package com.ytxg.book.controller;


import com.ytxg.book.dto.*;
import com.ytxg.core.dto.RestResp;
import com.ytxg.book.service.IBookInfoService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/front/book")
public class BookController {

    @Resource
    private IBookInfoService iBookInfoService;

    /**
     * 小说章节列表查询接口
     */
    @GetMapping("chapter/list")
    public RestResp<List<BookChapterRespDto>> listChapters(Long bookId) {
        return iBookInfoService.listChapters(bookId);
    }

    /**
     * 小说内容相关信息查询接口
     */
    @GetMapping("content/{chapterId}")
    public RestResp<BookContentAboutRespDto> getBookContentAbout(@PathVariable("chapterId") Long chapterId) {
        return iBookInfoService.getBookContentAbout(chapterId);
    }


    /**
     * 小说信息查询接口
     */

    @GetMapping("{id}")
    public RestResp<BookInfoRespDto> getBookById(@PathVariable("id") Long bookId) {
        RestResp<BookInfoRespDto> bookById = iBookInfoService.getBookById(bookId);
        return bookById;
    }
    /**
     * 点击榜单
     * @return
     */
    @GetMapping("visit_rank")
    public RestResp<List<BookRankRespDto>> visitRank() {

        RestResp<List<BookRankRespDto>> bookOrdByVisit = iBookInfoService.getBookOrdByVisit();

        return bookOrdByVisit;
    }

    /**
     * 新书榜单
     * @return
     */
        @GetMapping("newest_rank")
    public RestResp<List<BookRankRespDto>> newestRank() {
        RestResp<List<BookRankRespDto>> bookRank = iBookInfoService.getBookRank();

        return bookRank;
    }

    /**
     * 更新榜单
     * @return
     */
    @GetMapping("update_rank")
    public RestResp<List<BookRankRespDto>> updateRank(){
        RestResp<List<BookRankRespDto>> bookUpdateRank = iBookInfoService.getBookUpdateRank();
        return bookUpdateRank;
    }

    /**
     * 点击量增加
     * @return
     */
    @PostMapping("visit")
    public RestResp visit(@RequestBody BookVisitDto bookVisitDto){
        String id = bookVisitDto.getBookId();
        iBookInfoService.addVisit(Long.parseLong(id));
        return RestResp.ok();
    }

    /**
     * 同类推荐
     * @param bookId
     * @return
     */
    @GetMapping("rec_list")
    public RestResp recList(Long bookId){

        RestResp<List<BookInfoRespDto>> recList = iBookInfoService.getRecList(bookId);

        return recList;
    }

    /**
     * 评论
     * @param bookId
     * @return
     */
    @GetMapping("comment/newest_list")
    public RestResp newestList(Long bookId){
        RestResp<BookCommentDto> newestList = iBookInfoService.getNewestList(bookId);

        return newestList;
    }

    /**
     * 最新章节
     * @param bookId
     * @return
     */
    @GetMapping("last_chapter/about")
    public RestResp about(Long bookId){
        RestResp bookAbout = iBookInfoService.getBookAbout(bookId);

        return bookAbout;
    }

    /**
     * 作品分类
     * @param workDirection
     * @return
     */
    @GetMapping("category/list")
    public RestResp list(Integer workDirection){

        RestResp list = iBookInfoService.getList(workDirection);
        return list;
    }

    /**
     * 获取下一章章节id
     * @param id
     * @return
     */
    @GetMapping("next_chapter_id/{id}")
    public RestResp<Long> nextChapter(@PathVariable("id") Long id){
        RestResp<Long> nextChapter = iBookInfoService.getChapterId(id,1);

        return nextChapter;
    }
    @GetMapping("pre_chapter_id/{id}")
    public RestResp<Long> preChapter(@PathVariable("id") Long id){
        RestResp<Long> preChapter = iBookInfoService.getChapterId(id,-1);

        return preChapter;
    }
}
